package com.biaoyoo.www.personapp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.core.BaseViewHolder;

import java.util.ArrayList;

public class SimpleListActivity extends BaseListActivity<String> implements SwipeRefreshLayout.OnRefreshListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void loadViews() {
        setContentView(R.layout.activity_set,R.string.device_title);
        super.loadViews();
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mDataList = new ArrayList<>();
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });
    }

    @Override
    protected BaseViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_list_item, parent, false);
        return new SimpleViewHolder(view);
    }


    @Override
    public void onRefresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
//        mDataList.clear();
        int size = mDataList.size();
        for (int i = size; i < size+20; i++) {
            mDataList.add("sample list item " + i);
        }
        mAdapter.notifyDataSetChanged();
        mSwipeRefreshLayout.setRefreshing(false);
    }

    class SimpleViewHolder extends BaseViewHolder{

        TextView simpleItemTv;
        public SimpleViewHolder(View itemView) {
            super(itemView);
            simpleItemTv = (TextView) itemView.findViewById(R.id.tv_simeple_list_item);
        }

        @Override
        public void onBind(int position) {
            simpleItemTv.setText(mDataList.get(position).toString());
        }

    }
}
